package com.ouheng.shop.controller;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.annotation.Log;
import com.ouheng.common.config.Constant;
import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;
import com.ouheng.common.utils.ServletsUtils;
import com.ouheng.shop.domain.AxImageDO;
import com.ouheng.shop.domain.LebeProductDO;
import com.ouheng.shop.service.AxImageService;
import com.ouheng.shop.service.LebeProductService;

/**
 * 有币商品
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-09-14 11:13:28
 */
 
@Controller
@RequestMapping("/shop/lebeProduct")
public class LebeProductController {
	@Autowired
	private LebeProductService lebeProductService;
	
	@Autowired
	private AxImageService axImageService;
	
	@GetMapping()
	@RequiresPermissions("shop:lebeProduct:lebeProduct")
	String LebeProduct(){
	    return "shop/lebeProduct/lebeProduct";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:lebeProduct:lebeProduct")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LebeProductDO> lebeProductList = lebeProductService.list(query);
		int total = lebeProductService.count(query);
		PageUtils pageUtils = new PageUtils(lebeProductList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:lebeProduct:add")
	String add(){
	    return "shop/lebeProduct/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("shop:lebeProduct:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LebeProductDO lebeProduct = lebeProductService.get(id);
		model.addAttribute("lebeProduct", lebeProduct);
	    return "shop/lebeProduct/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:lebeProduct:add")
	public R save( LebeProductDO lebeProduct){
		if(lebeProductService.save(lebeProduct)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:lebeProduct:edit")
	public R update( LebeProductDO lebeProduct){
		lebeProductService.update(lebeProduct);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:lebeProduct:remove")
	public R remove( Long id){
		if(lebeProductService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:lebeProduct:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		lebeProductService.batchRemove(ids);
		return R.ok();
	}
	
	@Log("添加图片")
	@GetMapping("/image/{id}")
	@RequiresPermissions("shop:lebeProduct:image")
	String image(@PathVariable("id") Long id,Model model){
		LebeProductDO lebeProduct = lebeProductService.get(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pkid", id);
		map.put("type", 2);
		List<AxImageDO> listAxImageDO = axImageService.list(map);
		Map<String, Object> map1 = new HashMap<String, Object>();
		
		map1.put("pkid", id);
		map1.put("type", 4);
		map1.put("sort", "sort");
		map1.put("order","asc");
		List<AxImageDO> listAxImageDO1 = axImageService.list(map1);
		model.addAttribute("lebeProduct", lebeProduct);
		model.addAttribute("listAxImageDO", listAxImageDO);
		model.addAttribute("listAxImageDO1", listAxImageDO1);
		model.addAttribute("Oss_Url", Constant.Oss_Url);
	    return "shop/lebeProduct/image";
	}
	
	/**
	 * 
	 * @Title: saveImage   
	 * @Description: TODO(保存图片)   
	 * @param: @param product
	 * @param: @param request
	 * @param: @return      
	 * @return: R      
	 * @throws
	 */
	@Log("保存图片")
	@ResponseBody
	@PostMapping("/saveImage")
	@RequiresPermissions("shop:lebeProduct:edit")
	public R saveImage( LebeProductDO lebeProduct,HttpServletRequest request){
		List<Object> list;
		try {
			list = ServletsUtils.convertObjectList(AxImageDO.class, request,"attachs_");
			List<Object> list1 = ServletsUtils.convertObjectList(AxImageDO.class, request,"attachs1_");
			if(lebeProductService.saveImage(lebeProduct, list,list1)>0){
				return R.ok();
			}
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IntrospectionException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return R.error();
	}
	
}
